/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Celda.java
 *
 * Created on 27/05/2012, 01:06:59 AM
 */
package View;

import Controller.Calculadora;
import Controller.MainController;
import javax.swing.JOptionPane;
import Model.Celdam;
import Controller.MatrizDatos;
import Model.twoWays;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author Martin Kanayet
 */
public class Celda extends javax.swing.JFrame {

    /** Creates new form Celda */
    public Celda() {
        initComponents();
        jTextField1.setText("5");
        jTextField2.setText("0");
        jTextField3.setText("0.1975");
        jTextField4.setText("0.1349");
        jTextField5.setText("0.1349");        
    }

    public void Inicializar(String datos, String lambda1, String lambda2, String lambda3,
            String lambda4) {
        jTextField1.setText(datos);
        jTextField2.setText(lambda1);
        jTextField3.setText(lambda2);
        jTextField4.setText(lambda3);
        jTextField5.setText(lambda4);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        GuardarBoton = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();

        jLabel1.setText("Datos");

        jLabel2.setText("Lambda1");

        jLabel3.setText("Lambda2");

        jLabel4.setText("Lambda3");

        jLabel5.setText("Lambda4");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jTextField4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField4ActionPerformed(evt);
            }
        });

        GuardarBoton.setText("Guardar");
        GuardarBoton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GuardarBotonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(284, Short.MAX_VALUE)
                .addComponent(GuardarBoton)
                .addGap(45, 45, 45))
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE))
                        .addGap(90, 90, 90))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(jLabel6)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                .addComponent(GuardarBoton)
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField1ActionPerformed

private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField4ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField4ActionPerformed

private void GuardarBotonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GuardarBotonActionPerformed
    int out = 0;
    int datos;
    int rangoNumero = 99999;//rango del numero aleatorio
    double lambda1;
    double lambda2;
    double lambda3;
    double lambda4;
    double num = 0;
    ArrayList<Double> valores = new ArrayList<Double>();
    Celdam celdam = new Celdam();
    Random ran = new Random();
    if (out == 0) {
        try {
            datos = Integer.parseInt(jTextField1.getText());
            celdam.setDatos(datos);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Mal datos", "Mal", JOptionPane.ERROR_MESSAGE);
            out += 1;
            jLabel6.setText("ERROR CON SUS DATOS");
            return;
        }
    }
    if (out == 0) {
        try {
            lambda1 = Double.parseDouble(jTextField2.getText());
            celdam.setLambda1(lambda1);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Mal lambda1", "Mal", JOptionPane.ERROR_MESSAGE);
            out += 1;
            jLabel6.setText("ERROR CON SUS DATOS");
            return;
        }
    }
    if (out == 0) {
        try {
            lambda2 = Double.parseDouble(jTextField3.getText());
            celdam.setLambda2(lambda2);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Mal lambda2", "Mal", JOptionPane.ERROR_MESSAGE);
            out += 1;
            jLabel6.setText("ERROR CON SUS DATOS");
            return;
        }
    }
    if (out == 0) {
        try {
            lambda3 = Double.parseDouble(jTextField4.getText());
            celdam.setLambda3(lambda3);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Mal lambda3", "Mal", JOptionPane.ERROR_MESSAGE);
            out += 1;
            jLabel6.setText("ERROR CON SUS DATOS");
            return;
        }
    }
    if (out == 0) {
        try {
            lambda4 = Double.parseDouble(jTextField5.getText());
            celdam.setLambda4(lambda4);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Mal lambda4", "Mal", JOptionPane.ERROR_MESSAGE);
            out += 1;
            jLabel6.setText("ERROR CON SUS DATOS");
            return;
        }
    }
    if (out == 0) {
        Calculadora c = new Calculadora();

        for (int i = 0; i < celdam.getDatos(); i++) {
            num = c.generarDatos(twoWays.getGranMedia(), twoWays.getBeta(), Celdam.getPosX(), Celdam.getPosY(), celdam.getLambda1(), celdam.getLambda2(), celdam.getLambda3(), celdam.getLambda4());
            valores.add(num);
        }
        celdam.setValores(valores);
        MatrizDatos matrizDatos = new MatrizDatos();

        matrizDatos.agregarDato(celdam, Celdam.getPosX(), Celdam.getPosY(), twoWays.getI(), twoWays.getJ());
        
        jLabel6.setText("LA INFORMACION SE ALMACENO CORRECTAMENTE");

    }

}//GEN-LAST:event_GuardarBotonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Celda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Celda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Celda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Celda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Celda().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton GuardarBoton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables
}
